
/**
 * ES6 iterator
 */


 let arr2: any = [1, 2, 3];

 // 两种扩展
 // 原型扩展
(Array.prototype as any)[Symbol.iterator] = function () {
    let index = 0;
    let that = this;
    return {
        next() {
            // [Symbol.iterator]的函数中不能在next中打印，不然会报堆栈溢出
            // console.log('d');
            return index < that.length ? { value: that[index++], done: false } : { value: undefined, done: true };
        }
    }
}
// 直接在arr2上扩展
// arr2[Symbol.iterator] = function () {
//     let index = 0;
//     return {
//         next: () => {
//             return index < this.length ? { value: this[index++], done: false } : { value: undefined, done: true };
//         }
//     }
// } 


// array中的迭代器
let it2 = arr2[Symbol.iterator]();
console.log(it2.next());
console.log(it2.next());
console.log(it2.next());
console.log(it2.next());
//Map Set String arguments NodeList


/*
// string中的迭代器
let str = 'abc';
let it3 = str[Symbol.iterator]();
console.log(it3.next());
console.log(it3.next());
console.log(it3.next());
console.log(it3.next()); */
